package com.wman.sheep.common.application;

import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;

import com.wman.sheep.common.utils.TDevice;
import com.wman.sheep.common.utils.TextUtil;
import com.wman.sheep.okhttputils.OkHttpUtils;
import com.wman.sheep.okhttputils.model.HttpHeaders;
import com.wman.sheep.okhttputils.model.HttpParams;

public abstract class AbsAppContext extends Application {
    protected static Context _context;
    protected static Resources _resource;

    /**
     * Activity管理类
     */
    private static ActivityManagerImpl _activityManager;

    @Override
    public void onCreate() {
        super.onCreate();

        _context = getApplicationContext();
        _resource = _context.getResources();

        //初始化三方SDK
        initSocial();

        initOkHttp();

        initAppContext();
    }

    public static synchronized AbsAppContext context() {
        return (AbsAppContext) _context;
    }

    public static Resources resources() {
        return _resource;
    }


    /**
     * 获取全局Activity管理类
     */
    public final static ActivityManagerImpl getActivityManager() {
        if (_activityManager == null) {
            synchronized (ActivityManagerImpl.class) {
                if (_activityManager == null) {
                    _activityManager = new DefaultActivityManager();
                }
            }
        }
        return _activityManager;
    }

    /**
     * 初始化三方SDK
     */
    protected abstract void initSocial();

    protected abstract void initAppContext();

    /**
     * 初始化okhttp
     */
    private void initOkHttp() {
        HttpHeaders headers = new HttpHeaders();
        headers.put("v", TDevice.getVersionName());    //所有的 header 都 不支持 中文
        headers.put("device_id", getPseudoUniqueID());
        HttpParams params = new HttpParams();
        //必须调用初始化
        OkHttpUtils.init(this);
        //以下都不是必须的，根据需要自行选择
        OkHttpUtils.getInstance()//
                .debug("OkHttpUtils")                                   //是否打开调试
                .setConnectTimeout(10000)    //全局的连接超时时间
                .setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)       //全局的读取超时时间
                .setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)      //全局的写入超时时间
                .setCacheTime(60 * 60 * 24 * 7)                         //设置缓存时间一周
                .addCommonHeaders(headers);                             //设置全局公共头
    }

    /**
     * 获取当前网络类型
     *
     * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
     */

    // 手机网络类型
    public static final int NETTYPE_WIFI = 0x01;
    public static final int NETTYPE_CMWAP = 0x02;
    public static final int NETTYPE_CMNET = 0x03;

    public static int getNetworkType() {
        int netType = 0;
        ConnectivityManager connectivityManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            String extraInfo = networkInfo.getExtraInfo();
            if (!TextUtil.isEmpty(extraInfo)) {
                if (extraInfo.toLowerCase().equals("cmnet")) {
                    netType = NETTYPE_CMNET;
                } else {
                    netType = NETTYPE_CMWAP;
                }
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = NETTYPE_WIFI;
        }
        return netType;
    }

    /**
     * 设备ID
     *
     * @return
     */
    public static String getPseudoUniqueID() {
        String m_szDevIDShort = "35" +
                Build.BOARD.length() % 10 +
                Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 +
                Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 +
                Build.HOST.length() % 10 +
                Build.ID.length() % 10 +
                Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 +
                Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 +
                Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 digits

        return m_szDevIDShort;
    }
}
